//
//  PayTableViewController.m
//  LoveFreshBeen
//
//  Created by 胡不归是大虾 on 2016/10/16.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "PayTableViewController.h"
#import "CartBottomView.h"
#import "PayTypeCell.h"
#import "HMHomeFirstModel.h"
#import "ShopCartGoodsListTool.h"
#import "ChoosedGoodsCell.h"
#import "chargeDetailCell.h"
@interface PayTableViewController ()<CartBottomViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,weak) CartBottomView* bottomView;
@property(nonatomic,weak) UITableView* tableView;
@property(nonatomic,strong)NSArray* modelArray;
@property(nonatomic,assign)CGFloat totalPrice;
@end

@implementation PayTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView* tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView = tableView;
    self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
    self.tableView.sectionFooterHeight = 15;
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];

    self.navigationItem.title = @"结算付款";
    UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
    [btn setBackgroundImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@49);
    }];
    self.bottomView.actualPrice = [self getActualPrice];
    //设置默认选中
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] animated:NO scrollPosition:UITableViewScrollPositionNone];

}
-(void)viewWillAppear:(BOOL)animated{
    self.bottomView.hidden = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    self.bottomView.hidden = YES;
}
    
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger rowCount = 0;
    switch (section) {
        case 0:
            rowCount = 1;
            break;
        case 1:
            rowCount = 4;
            break;
        case 2:
            rowCount = self.modelArray.count + 1;
            break;
        case 3:
            rowCount = 1;
            break;
        default:
            break;
    }
    return rowCount;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell ;
    if (indexPath.section == 0) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"discountCell" owner:nil options:nil] lastObject];
    }
    if (indexPath.section == 1) {
        PayTypeCell* cell1 = [[[NSBundle mainBundle] loadNibNamed:@"PayTypeCell" owner:nil options:nil] lastObject];
        cell1.row = indexPath.row;
        cell =cell1;
    }
    if (indexPath.section == 2) {

        if (indexPath.row == self.modelArray.count ) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"totalPriceCell" owner:nil options:nil] lastObject];
            UILabel* lable = [cell viewWithTag:10];
            lable.text = [NSString stringWithFormat:@"$%.1f",self.totalPrice];
        }else
        {
        ChoosedGoodsCell* choosedCell = [[[NSBundle mainBundle] loadNibNamed:@"ChoosedGoodsCell" owner:nil options:nil] lastObject];
        choosedCell.model = self.modelArray[indexPath.row];
        cell = choosedCell;
        }
    }
    if (indexPath.section == 3) {

        chargeDetailCell* chargeCell = [[[NSBundle mainBundle] loadNibNamed:@"chargeDetailCell" owner:nil options:nil] lastObject];
        chargeCell.price = self.totalPrice;
        cell = chargeCell;
        }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewCell* cell = [[UITableViewCell alloc] init];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.textColor = [UIColor lightGrayColor];
    cell.backgroundColor = [UIColor whiteColor];
    switch (section) {
        case 1:
            cell.textLabel.text = @"选择支付方式";
            break;
        case 2:
            cell.textLabel.text = @"精选商品";
            break;
        case 3:
            cell.textLabel.text = @"费用明细";
            break;
        default:
            break;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    else {
        return 35;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3) {
        return 120;
    }else{
        return 40;
    }
}

-(NSArray *)modelArray{
    if (!_modelArray) {
        _modelArray = [ShopCartGoodsListTool sharedGoodsListTool].goodsList;
    }
    return _modelArray;
}
-(CGFloat)totalPrice{
    CGFloat price = 0.0;
    for (HMHomeFirstModel* model in self.modelArray) {
        price += model.goodsCount * model.partner_price;
    }
    return price;
}
-(CartBottomView *)bottomView{
    if (!_bottomView) {
        CartBottomView* bottomView = [[[NSBundle mainBundle] loadNibNamed:@"CartBottomView" owner:nil options:nil] lastObject];
        bottomView.type = CartBottomViewPay;
        _bottomView = bottomView;
        bottomView.delegate = self;
        [self.view addSubview:bottomView];
    }
    return _bottomView;
}
-(CGFloat)getActualPrice{
    CGFloat actualPrice = self.totalPrice;
    if (self.totalPrice >= 39 ) {
        actualPrice -= 5;
    }
    if (self.totalPrice < 100) {
        actualPrice += 8;
    }
    return actualPrice;
}
@end
